package com.ruoyi.epidemic.web;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.epidemic.domain.TbDetails;
import com.ruoyi.epidemic.domain.TbUser;
import com.ruoyi.epidemic.service.ITbDetailsService;
import com.ruoyi.epidemic.service.ITbUserService;
import com.ruoyi.epidemic.vo.PasswordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author zyc
 * @createDate 2023/3/21 10:16
 */
@RestController
@RequestMapping("/web/user")
public class UserController {

    @Autowired
    private ITbUserService userService;
    @Autowired
    private ITbDetailsService detailsService;

    //用户头像上传
    @RequestMapping("/upload")
    public String upload(@RequestParam MultipartFile file) throws IOException {
       return userService.upload(file);
    }

    //用户登陆
    @PostMapping("/login")
    public AjaxResult login(@RequestBody TbUser user){
            return userService.login(user);
    }
    //用户注册
    @PostMapping("/register")
    public AjaxResult register(@RequestBody TbUser user){
        return userService.register(user);
    }

    //修改个人信息
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody TbUser tbUser)
    {
        int i = userService.updateTbUser(tbUser);
        if (i>0){
            TbUser tbUser1 = userService.selectTbUserById(tbUser.getId());
            return AjaxResult.success(tbUser1);
        }
        return AjaxResult.error("修改失败");
    }


    //用户登陆弹窗提示
    @GetMapping("/dialog")
    public AjaxResult dialog(@RequestParam Long userId){
        TbUser tbUser = userService.selectTbUserById(userId);
        String city = tbUser.getCity();
        if (StringUtils.isEmpty(city)){
            return AjaxResult.success("用户城市信息未填写，暂无疫情数据");
        }else {
            String substring = city.substring(0, city.length() - 1);
            System.out.println("====================="+substring);
            TbDetails details = detailsService.selectByCity(substring);
            if (details==null){
                return AjaxResult.success("系统中暂无该城市疫情数据");
            }
            Long confirm = details.getConfirm();
            Long heal = details.getHeal();
            Long dead = details.getDead();
            Long asymptomatic = details.getAsymptomatic();
            Long overseas = details.getOverseas();
            return AjaxResult.success(city+"当前确诊人数:"+confirm+",死亡人数:"+dead
                    +",无症状感染者人数:"+asymptomatic+"境外输入人数:"+overseas+"累计治愈人数:"+heal);
        }
    }

    //用户修改密码
    @PostMapping("/updatepassword")
    public AjaxResult edit(@RequestBody PasswordVo passwordVo)
    {
        TbUser tbUser = userService.selectTbUserById(passwordVo.getId());
        if (StringUtils.isEmpty(passwordVo.getNewPassword())){
            return AjaxResult.error("新密码不能为空");
        }
        if (!StringUtils.isEmpty(passwordVo.getOldPassword())) {
            if (passwordVo.getOldPassword().equals(tbUser.getPassword())) {
                TbUser newUser = new TbUser();
                newUser.setId(passwordVo.getId());
                newUser.setPassword(passwordVo.getNewPassword());
                int i = userService.updateTbUser(newUser);
                if (i>0){
                    TbUser user = userService.selectTbUserById(passwordVo.getId());
                    return AjaxResult.success(user);
                }else {
                    return AjaxResult.error("修改密码失败");
                }
            }else {
                return AjaxResult.error("旧密码错误,请重新输入");
            }
        }
        return AjaxResult.error("旧密码为空");
    }
}
